/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test;

import static org.junit.Assert.assertEquals;
import static org.sourceprojects.lycia.fluent.FluentBuilder.generationStrategy;
import static org.sourceprojects.lycia.fluent.FluentBuilder.method;
import static org.sourceprojects.lycia.fluent.FluentBuilder.prepare;
import static org.sourceprojects.lycia.fluent.FluentBuilder.tag;
import static org.sourceprojects.lycia.fluent.FluentBuilder.validateSchema;

import java.lang.reflect.Method;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.GenerationStrategy;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.annotations.Base64;
import org.sourceprojects.lycia.annotations.ContextObject;
import org.sourceprojects.lycia.annotations.TextBody;
import org.sourceprojects.lycia.exceptions.LyciaConfigurationException;
import org.sourceprojects.lycia.fluent.FluentBuilder;
import org.w3c.dom.Element;

public class BytecodeConfigurableAnnotationsTestCase {
	private static final String XML_DATA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xml><input id=\"5\" encoded=\"VGVzdHN0cmluZw==\">testvalue</input></xml>";
	private static final String XML_DATA_CDATA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xml><input id=\"5\" encoded=\"VGVzdHN0cmluZw==\"><![CDATA[testvalue]]></input></xml>";

	@Test
	public void testConfigurableAnnotations() throws Exception {
		final FluentBuilder<StringBuilder> builder = prepare();

		final Method method = getClass().getMethod("parseData", Element.class,
				StringBuilder.class, int.class, String.class, byte[].class);

		builder.parser(tag("input", method(method, this)));

		builder.configure(validateSchema(false),
				generationStrategy(GenerationStrategy.BytecodeGeneration));

		final LyciaParser<StringBuilder> xmlParser = builder.build();
		xmlParser.getParserContext().setContextObject(new StringBuilder());

		xmlParser.parse(XML_DATA);

		final StringBuilder sb = xmlParser.getParserContext()
				.getContextObject();
		assertEquals("namespace=null;id=5;content=testvalue;"
				+ "decoded=Teststring", sb.toString());
	}

	@Test
	public void testConfigurableAnnotationsCDATA() throws Exception {
		final FluentBuilder<StringBuilder> builder = prepare();

		final Method method = getClass().getMethod("parseCData", Element.class,
				StringBuilder.class, int.class, String.class, byte[].class);

		builder.parser(tag("input", method(method, this)));

		builder.configure(validateSchema(false),
				generationStrategy(GenerationStrategy.BytecodeGeneration));

		final LyciaParser<StringBuilder> xmlParser = builder.build();
		xmlParser.getParserContext().setContextObject(new StringBuilder());

		xmlParser.parse(XML_DATA_CDATA);

		final StringBuilder sb = xmlParser.getParserContext()
				.getContextObject();
		assertEquals("namespace=null;id=5;content=testvalue;"
				+ "decoded=Teststring", sb.toString());
	}

	@Test(expected = LyciaConfigurationException.class)
	public void testConfigurableAnnotationsFail() throws Exception {
		final FluentBuilder<StringBuilder> builder = prepare();

		final Method method = getClass().getMethod("failParseData",
				StringBuilder.class);

		builder.parser(tag("input", method(method, this)));

		builder.configure(validateSchema(false),
				generationStrategy(GenerationStrategy.BytecodeGeneration));

		final LyciaParser<StringBuilder> xmlParser = builder.build();
		xmlParser.getParserContext().setContextObject(new StringBuilder());

		xmlParser.parse(XML_DATA);
	}

	@Test
	public void testConfigurableAnnotationsBytecodeAttributename()
			throws Exception {
		final FluentBuilder<StringBuilder> builder = prepare();

		final Method method = getClass().getMethod(
				"parseDataBytecodeAttributename", Element.class,
				StringBuilder.class, int.class, String.class, byte[].class);

		builder.parser(tag("input", method(method, this)));

		builder.configure(validateSchema(false),
				generationStrategy(GenerationStrategy.BytecodeGeneration));

		final LyciaParser<StringBuilder> xmlParser = builder.build();
		xmlParser.getParserContext().setContextObject(new StringBuilder());

		xmlParser.parse(XML_DATA);

		final StringBuilder sb = xmlParser.getParserContext()
				.getContextObject();
		assertEquals("namespace=null;id=5;content=testvalue;"
				+ "decoded=Teststring", sb.toString());
	}

	@Test
	public void testConfigurableAnnotationsCDATABytecodeAttributename()
			throws Exception {
		final FluentBuilder<StringBuilder> builder = prepare();

		final Method method = getClass().getMethod(
				"parseCDataBytecodeAttributename", Element.class,
				StringBuilder.class, int.class, String.class, byte[].class);

		builder.parser(tag("input", method(method, this)));

		builder.configure(validateSchema(false),
				generationStrategy(GenerationStrategy.BytecodeGeneration));

		final LyciaParser<StringBuilder> xmlParser = builder.build();
		xmlParser.getParserContext().setContextObject(new StringBuilder());

		xmlParser.parse(XML_DATA_CDATA);

		final StringBuilder sb = xmlParser.getParserContext()
				.getContextObject();
		assertEquals("namespace=null;id=5;content=testvalue;"
				+ "decoded=Teststring", sb.toString());
	}

	public void parseData(final Element element,
			@ContextObject final StringBuilder stringBuilder,
			@Attribute("id") final int id, @TextBody final String body,
			@Attribute("encoded") @Base64 final byte[] data) {

		stringBuilder.append("namespace=").append(element.getNamespaceURI())
				.append(';');
		stringBuilder.append("id=").append(id).append(';');
		stringBuilder.append("content=").append(body).append(';');
		stringBuilder.append("decoded=").append(new String(data));
	}

	public void parseCData(final Element element,
			@ContextObject final StringBuilder stringBuilder,
			@Attribute("id") final int id, @TextBody final String body,
			@Attribute("encoded") @Base64 final byte[] data) {

		stringBuilder.append("namespace=").append(element.getNamespaceURI())
				.append(';');
		stringBuilder.append("id=").append(id).append(';');
		stringBuilder.append("content=").append(body).append(';');
		stringBuilder.append("decoded=").append(new String(data));
	}

	public void failParseData(
			@Base64 @ContextObject final StringBuilder stringBuilder) {
		stringBuilder.append("");
	}

	public void parseDataBytecodeAttributename(final Element element,
			@ContextObject final StringBuilder stringBuilder,
			@Attribute final int id, @TextBody final String body,
			@Attribute @Base64 final byte[] encoded) {

		stringBuilder.append("namespace=").append(element.getNamespaceURI())
				.append(';');
		stringBuilder.append("id=").append(id).append(';');
		stringBuilder.append("content=").append(body).append(';');
		stringBuilder.append("decoded=").append(new String(encoded));
	}

	public void parseCDataBytecodeAttributename(final Element element,
			@ContextObject final StringBuilder stringBuilder,
			@Attribute final int id, @TextBody final String body,
			@Attribute @Base64 final byte[] encoded) {

		stringBuilder.append("namespace=").append(element.getNamespaceURI())
				.append(';');
		stringBuilder.append("id=").append(id).append(';');
		stringBuilder.append("content=").append(body).append(';');
		stringBuilder.append("decoded=").append(new String(encoded));
	}

}
